package io.renren.modules.live.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import io.renren.modules.live.entity.FanweUserEntity;
import io.renren.modules.live.service.FanweAnchorService;
import io.renren.modules.live.service.FanweUserService;

import io.renren.modules.live.vo.UpdateLoginPwdVo;
import io.renren.modules.live.vo.UserAnchorUpdateInfoVo;
import io.renren.modules.live.vo.UserListVo;

import io.renren.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.codec.digest.DigestUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import java.util.Arrays;
import java.util.Map;




/**
 * 用户信息
 *
 * @author shaoqi
 * @email sunlightcs@gmail.com
 * @date 2020-11-17 09:18:22
 */
@Api(tags={"用户管理接口"})
@RestController
@RequestMapping("live/fanweuser")
@Log4j2
public class FanweUserController extends AbstractController {
    @Autowired
    private FanweUserService fanweUserService;

    @Autowired
    private FanweAnchorService anchorService;



    /**
     * 佟卢海 - 2020.11.17
     *
     * 管理员查看用户列表
     */
    @PostMapping("/userList")
    @ApiOperation("管理员查看用户列表")
    //@RequiresPermissions("liveAdministration:user")
    public R selectUserList(@RequestBody UserListVo userListVo){
        R result=new R();
        try{
            if(userListVo.getPageIndex()==null || userListVo.getPageSize() ==null){
                result.put("code",9999);
                result.put("msg","页码页容不能为空！！");
                result.put("data",null);
                return result;
            }
            Map<String, Object> stringObjectMap = fanweUserService.selectUserList(userListVo);
            result.put("data",stringObjectMap);
        }catch (Exception e){
            log.info(e);
            result.put("code",9999);
            result.put("msg","查询失败");
            result.put("data",null);
        }
        return result;
    }
    /**
     * 佟卢海 - 2020.11.18
     *
     * 通过ID查询用户信息
     */
    @GetMapping("/selectUserByid")
    @ApiOperation("通过ID查询用户信息")
    //@RequiresPermissions("liveAdministration:user")
    public R selectUserByid(@ApiParam(value = "用户ID",required = true) Long userId){
        R result=new R();
        try{
            if(userId==null ){
                result.put("code",9999);
                result.put("msg","id不能为空！！");
                result.put("data",null);
            }
            FanweUserEntity fanweUserEntity = fanweUserService.selectUserByid(userId);
            result.put("data",fanweUserEntity);
        }catch (Exception e){
            log.info(e);
            result.put("code",9999);
            result.put("msg","查询失败");
            result.put("data",null);
        }
        return result;
    }

  /**
     * 列表
     */
    @ApiOperation("查询所有用户信息")
    @GetMapping("/list")
    //@RequiresPermissions("live:fanweuser:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = fanweUserService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 修改
     */
    @ApiOperation("用户修改登录密码")
    @PostMapping("/updateLoginPwd")
    //@RequiresPermissions("live:fanweuser:update")
    public R updateLoginPwd(@RequestBody UpdateLoginPwdVo updateLoginPwdVo){
        if(updateLoginPwdVo==null){
            return R.error("请输入要更改的信息");
        }
        if(updateLoginPwdVo.getOldUserPwd()==null || "".equals(updateLoginPwdVo.getOldUserPwd())){
            return R.error("原密码不能为空!!");
        }
        if(updateLoginPwdVo.getNewUserPwd()==null || "".equals(updateLoginPwdVo.getNewUserPwd())){
            return R.error("新密码不能为空!!");
        }
        if(updateLoginPwdVo.getNewUserPwd()!=null&&updateLoginPwdVo.getNewUserPwd()!=""){
            String newPwd = DigestUtils.sha256Hex(updateLoginPwdVo.getNewUserPwd());
            //根据用户ID查询用户数据库原密码 比较
            FanweUserEntity userEntity = fanweUserService.getById(updateLoginPwdVo.getUserId());
            String old = DigestUtils.sha256Hex(updateLoginPwdVo.getOldUserPwd());
            if(userEntity.getUserPwd().equals(old)){
                fanweUserService.updatePwd(updateLoginPwdVo.getUserId(),newPwd);
            }else {
                return R.error("原密码输入错误!!!");
            }
            return R.ok("修改密码成功");
        }else {
            return R.error("请输入要修改的密码");
        }

    }

    /**
     * 修改
     */
    @ApiOperation("用户修改支付密码")
    @PostMapping("/updatePayPwd")
    //@RequiresPermissions("live:fanweuser:update")
    public R updatePayPwd(@RequestBody UpdateLoginPwdVo updateLoginPwdVo){
        String newPwd = DigestUtils.sha256Hex(updateLoginPwdVo.getNewUserPwd());
        //根据用户ID查询用户数据库原密码 比较
        FanweUserEntity userEntity = fanweUserService.getById(updateLoginPwdVo.getUserId());
        String old = DigestUtils.sha256Hex(updateLoginPwdVo.getOldUserPwd());
        if(userEntity.getPaypassword().equals(old)){
            fanweUserService.updatePayPwd(updateLoginPwdVo.getUserId(),newPwd);
        }else {
            return R.error("原密码输入错误!!!");
        }
        return R.ok("修改密码成功");
    }


    @ApiOperation("用户/主播修改个人信息")
    @PostMapping("/userAnchorUpdateInfo")
    public R userAnchorUpdateInfo(@RequestBody UserAnchorUpdateInfoVo userAnchorUpdateInfoVo,
                                  HttpServletRequest request){
        if (userAnchorUpdateInfoVo==null){
            return R.error("请输入您要改的信息");
        }
        String token = request.getHeader("token");
        String s = token.substring(0, 3);
        if(s.equals("usr")){
            fanweUserService.updateUserById(userAnchorUpdateInfoVo);
            return R.ok("修改成功");
        }else if(s.equals("anc")){
            anchorService.updateUserById(userAnchorUpdateInfoVo);
            return R.ok("修改成功");
        }else {
            return R.error("信息修改失败");
        }
    }

    /**
     * 保存
     */
    @ApiOperation("保存/添加一个用户")
    @PostMapping("/save")
    //@RequiresPermissions("live:fanweuser:save")
    public R save(@RequestBody FanweUserEntity fanweUser){
		fanweUserService.save(fanweUser);
        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation("修改用户")
    @PutMapping("/update")
   // @RequiresPermissions("live:fanweuser:update")
    public R update(@RequestBody FanweUserEntity fanweUser){
		fanweUserService.updateById(fanweUser);
        return R.ok();
    }



    /**
     * 删除
     */
    @ApiOperation("根据ID数组批量删除用户")
    @DeleteMapping("/delete")
    //@RequiresPermissions("live:fanweuser:delete")
    public R delete(@RequestBody Long[] ids){
		fanweUserService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
